package DAO.Impl;

import DAO.UserInfoDAO;
import domain.UserInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 数据库连接操作层实现类
 * @author: hank
 * @create: 2021-03-28 21:25
 **/
@SuppressWarnings("JavaDoc")
public class UserInfoDAOImpl implements UserInfoDAO {
    private final Connection conn;
    private PreparedStatement pstmt;

    public UserInfoDAOImpl(Connection conn) {
        this.conn = conn;
    }

    public int saveUserInfo(String UserName, String UserID, String UserYear, String UserMajor) throws SQLException {
        String sql = "INSERT into user_info VALUES (?,?,?,?)";
        this.pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, UserName);
        pstmt.setString(2, UserID);
        pstmt.setString(3, UserYear);
        pstmt.setString(4, UserMajor);
        int OK = this.pstmt.executeUpdate();
        return OK;
    }

    public List<UserInfo> getUserInfo(String user) throws SQLException {
        String sql="SELECT * FROM user_info WHERE user_id = ?";
        this.pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, user);
        ResultSet rs = this.pstmt.executeQuery();
        List<UserInfo> all = new ArrayList<UserInfo>();
        while (rs.next()) {
            UserInfo vo = new UserInfo();
            vo.setUser_Name(rs.getString(1));
            vo.setUser_id(rs.getString(2));
            vo.setUser_Year(rs.getString(3));
            vo.setUser_Major(rs.getString(4));
            all.add(vo);
        }
        return all;
    }
}
